﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestReadDatFileDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Task.Run(() =>
            {
                #region 读取单个二进制文件

                string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Data\1\192.168.2.64_0_0\0\0_1000.dat");
                //string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Data\ChannelBuffer\B192_168_2_64_0_20241013102815161.buf");

                FileInfo fileInfo = new FileInfo(path);
                long lcount = fileInfo.Length; //文件占byte字节数
                long dataLength = lcount / sizeof(float);  //1个float占4个byte

                byte[] bytedatas = new byte[lcount];
                float[] tmpDatas = new float[dataLength];

                using (FileStream fs = new FileStream(path, FileMode.Open))
                {
                    fs.Position = 0;
                    fs.Read(bytedatas, 0, (int)lcount);
                    try
                    {
                        Buffer.BlockCopy(bytedatas, 0, tmpDatas, 0, (int)lcount);
                    }
                    catch (Exception ex)
                    {
                        string msg = ex.Message;
                    }
                    finally
                    {
                        GC.Collect();
                    }
                }

                var temp = tmpDatas;

                #endregion
            });
        }

        private void button2_Click(object sender, EventArgs e)
        {
            #region 并行读取多个二进制文件

            List<string> strLists = new List<string>();
            string path1 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Data\1\192.168.2.64_0_0\0\0_1000.dat");
            string path2 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Data\1\192.168.2.64_0_1\0\0_1000.dat");
            string path3 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Data\1\192.168.2.64_0_2\0\0_1000.dat");
            strLists.Add(path1);
            strLists.Add(path2);
            strLists.Add(path3);

            //设置并行最大处理数
            ParallelOptions parallelOptions = new ParallelOptions();
            parallelOptions.MaxDegreeOfParallelism = 4; //并行最大处理数

            //并行处理，后台自动使用多线程处理
            Parallel.ForEach(strLists, parallelOptions, str =>
            {
                FileInfo fileInfo = new FileInfo(str);
                long lcount = fileInfo.Length; //文件占byte字节数
                long dataLength = lcount / sizeof(float);  //1个float占4个byte

                byte[] bytedatas = new byte[lcount];
                float[] tmpDatas = new float[dataLength];

                using (FileStream fs = new FileStream(str, FileMode.Open))
                {
                    fs.Position = 0;
                    fs.Read(bytedatas, 0, (int)lcount);
                    try
                    {
                        Buffer.BlockCopy(bytedatas, 0, tmpDatas, 0, (int)lcount);
                        Debug.WriteLine(str + "---" + tmpDatas.Length + "---" + tmpDatas[0]);
                    }
                    catch (Exception ex)
                    {
                        string msg = ex.Message;
                    }
                    finally
                    {
                        GC.Collect();
                    }
                }

                //button2.Text = tmpDatas.ToString();
            });

            #endregion
        }
    }
}
